﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cocon90.Lib.Util
{
    /// <summary>
    /// 数据类型安全转换
    /// </summary>
    public class parseHelper
    {
        /// <summary>
        /// 默认失败则返回-1
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static int toInt(string num, int defaultValue = -1)
        {
            int temp = defaultValue;
            if (int.TryParse(num, out temp)) return temp;
            else return defaultValue;
        }
        /// <summary>
        ///默认失败则返回-1.0000d
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static double toDouble(string num, double defaultValue = -1.0000d)
        {
            double temp = defaultValue;
            if (double.TryParse(num, out temp)) return temp;
            else return defaultValue;
        }
        /// <summary>
        /// 默认失败则返回-1.000f
        /// </summary>
        /// <param name="floatString"></param>
        /// <returns></returns>
        public static float toFloat(string floatString, float defaultValue = -1.000f)
        {
            float temp = defaultValue;
            if (float.TryParse(floatString, out temp)) return temp;
            else return defaultValue;
        }
        /// <summary>
        /// 默认转换失败则返回Null
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime? toDateTime(string date, DateTime? defaultValue = null)
        {
            DateTime temp;
            if (DateTime.TryParse(date, out temp)) return temp;
            return defaultValue;
        }
        /// <summary>
        /// 默认转换失败时反回此该起的100年前
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime toDateTimeLow(string date)
        {
            DateTime temp;
            if (DateTime.TryParse(date, out temp)) return temp;
            return DateTime.Now.AddYears(-100);
        }
        /// <summary>
        ///  默认转换失败时反回此该起的100年后
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime toDateTimeHigh(string date)
        {
            DateTime temp;
            if (DateTime.TryParse(date, out temp)) return temp;
            return DateTime.Now.AddYears(+100);
        }
        /// <summary>
        /// 对Bool类型进行转换，boolString为True的语句是："on","1","true","yes" 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool toBool(string boolString)
        {
            boolString = boolString + "";
            if (boolString.ToLower() == "on" || boolString.ToLower() == "1" || boolString.ToLower() == "true" || boolString.ToLower() == "yes") return true;
            else return false;
        }
        /// <summary>
        /// 将一个数字字符串，转为指定长度的数据字符串，比如：199转为4位长度为：0199,如果是12345转为2位，则为12
        /// </summary>
        /// <param name="num"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string toFixLength(string num, int length)
        {
            if (length - num.Length > 0)
                return new String('0', length - num.Length) + num;
            else
            {
                return num.Substring(0, length);
            }
        }
        /// <summary>
        /// 将一个字符串，转为指定长度的数据字符串，可以在前面加指定的字符
        /// </summary>
        /// <param name="num"></param>
        /// <param name="length"></param>
        /// <param name="padChar"></param>
        /// <returns></returns>
        public static string toFixLength(string num, int length, char padChar)
        {
            if (length - num.Length > 0)
                return new String(padChar, length - num.Length) + num;
            else
            {
                return num.Substring(0, length);
            }
        }
        /// <summary>
        /// 判断一个数字字符串是否是标准数字(包括正负int，double，decimal)。
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static bool isNumber(string num)
        {
            Decimal dec;
            return Decimal.TryParse(num, out dec);
        }

    }
}
